'use strict';

// Utils Function.
var setUIImageFrameByName = function (sprite, name, plist) {
    for (var i = 0; i < 3; i++) {
        var frame = cc.spriteFrameCache.getSpriteFrame(name);
        if (!frame) {
            cc.log('setUIImageFrameByName = ' + mCard.getCardstr() + ', name = ' + name);
            cc.spriteFrameCache.addSpriteFrames(zipai.res[mCard.getCardstr()+'_plist']);
        } else {
            sprite.loadTexture(name, ccui.Widget.PLIST_TEXTURE);
            return;
        }
    }
    if (!cc.sys.isNative)
        throw new Error('getSpriteFrame(' + name + ') == null');
    else
        showAlert('getSpriteFrame(' + name + ') == null');
};

(function (exports) {
    var Card = ccui.ImageView.extend({
        ctor: function () {
            this._super();
            //var imgSrc = mCard.getCardImg('mid_1');
            //setUIImageFrameByName(this, imgSrc, mCard.getCardstr());
            this.isSelected = false;
            return this;
        },

        //type 0中 1大 2小 3(34*34)
        setData: function (data, p, typ, isWei) {
            this.p = p;
            var visible = true;
            if (data == null) visible = false;
            data = parseInt(data);
            if (data < 0) {
                data = 0;
            } else if (data > 100) {
                data = 21;
            }
            this.data = data;
            this.typ = typ;
            this.isWei = isWei;
            this.backBg = false;
            this.resetCardImg();
            if (typ == 1) {
                //大牌加蓝边
                var bigSize = mCard.getBigSize();
                this.setContentSize(bigSize.w, bigSize.h);
            } else if (typ == 2) {
                var smallSize = mCard.getSmallSize();
                this.setContentSize(smallSize.w, smallSize.h);
            } else if (typ == 3) {
                var small2Size = mCard.getSmall2Size();
                this.setContentSize(small2Size.w, small2Size.h);
            } else if (typ == 4) {
                var backSize = mCard.getBackSize();
                this.setContentSize(backSize.w, backSize.h);
            } else {
                var cardSize = mCard.getCardSize();
                this.setContentSize(cardSize.w, cardSize.h);
            }
            this.setVisible(visible);
        },
        setRowColumn: function (row, column) {
            this.row = row;
            this.column = column;
        },
        getRow: function () {
            return this.row;
        },
        getColumn: function () {
            return this.column;
        },
        hideChiCard: function () {
            var select = this.getChildByName('chi');
            if (select) {
                select.setVisible(false);
            }
        },
        showChiCard: function () {
            var select = this.getChildByName('chi');
            if (select) {
                select.setVisible(true);
            }
        },
        setMo: function (flag) {
            var light = this.getChildByName('light');
            if (light == null) {
                light = new cc.Sprite(zipai.res.chupai_bj);
                light.setName('light');
                this.addChild(light);
            }
            var bigSize = this.data?cc.size(mCard.getBigSize().w,mCard.getBigSize().h):this.getContentSize();
            var size = light.getContentSize();
            light.setScaleX(bigSize.width/(size.width - (this.data>0?10:0)));
            light.setScaleY(bigSize.height/(size.height - (this.data>0?10:0)));
            light.setPosition(cc.p(bigSize.width / 2, bigSize.height / 2));

            light.setVisible(true);
            light.setTexture(flag ? zipai.res.mopai_bj : zipai.res.chupai_bj);
        },

        setSelectedCard: function (theCard) {
            this.selectedCard = theCard;

            this.setTouchEnabled(true);
            this.setSwallowTouches(true);
            if (ZiPaiRoomData.isReplay != true) {
                this.addTouchEventListener(this.onSelect, this);
            }
        },
        setTouchEnableStatus: function (flag) {
            if (this) {
                this.setTouchEnabled(flag);
            }
        },
        setGrayCard: function (flag) {
            var gray = this.getChildByName('gray');
            if (!gray) {
                var size = this.getContentSize();
                if (size.width == 98 && size.height == 122) {
                    gray = new cc.Sprite(zipai.res.cardmask);
                    gray.setAnchorPoint(cc.p(0,0));
                }
                if(size.width == 0){
                    gray = new cc.LayerColor();
                    gray.setColor(cc.color(70, 70, 70));
                    gray.setOpacity(150);
                    var cardSize = mCard.getOriginalSize();
                    size = cc.size(cardSize.w,cardSize.h);
                }
                gray.setContentSize(size);
                gray.setPosition(cc.p(0, 0));
                gray.setName('gray');
                this.addChild(gray);
            }
            gray.setVisible(flag);
        },

        /**
         * 给牌添加绿色图层（当前选择的牌，牌面上有的都变为绿色）
         * @param flag
         */
        setSelectCard: function (flag) {
            var select = this.getChildByName('select');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(38, 160, 100));
                select.setOpacity(150);
                var size = this.getContentSize();
                if(size.width == 0){
                    var cardSize = mCard.getSmallSize();
                    size = cc.size(cardSize.w,cardSize.h);
                }
                select.setContentSize(size);
                select.setPosition(cc.p(0, 0));
                select.setName('select');
                this.addChild(select);
            }
            select.setVisible(flag);
        },
        /**
         * 给牌添加红色图层 （玩家吃的那张牌标红）
         */
        setChiCard: function () {
            var select = this.getChildByName('chi');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(250, 0, 0));
                select.setOpacity(80);
                var size = this.getContentSize();
                if(size.width == 0){
                    var cardSize = mCard.getSmallSize();
                    size = cc.size(cardSize.w,cardSize.h);
                }
                select.setContentSize(size);
                select.setPosition(cc.p(0, 0));
                select.setName('chi');
                this.addChild(select);
            }
            select.setVisible(true);
        },
        resetCardImg: function () {
            var typ = this.typ;
            var data = this.data;
            var cardname = '';
            if (typ == 1) {
                cardname = 'big_' + data;
            } else if (typ == 2) {
                cardname = 'small_' + data;
            } else if (typ == 3) {
                cardname = 'small2_' + data;
            } else if (typ == 4) {
                cardname = 'back_' + data;
            } else {
                cardname = 'mid_' + data;
            }
            //如果是0  显示牌背
            if (data == 0) {
                this.backBg = true;
                this.resetCardBackImg();
            } else if (data >= 21) {
                setUIImageFrameByName(this, cardname + '.png', mCard.getCardstr());
            }else {
                var imgSrc = mCard.getCardImg(cardname);
                if (this.typ == 2 && this.isWei == true) {
                    this.backBg = true;
                    this.resetCardBackImg();
                } else {
                    this.imgSrc = imgSrc;
                    setUIImageFrameByName(this, imgSrc, mCard.getCardstr());
                }
            }
        },
        resetCardBackImg: function () {
            if (!this.backBg) {
                return;
            }
            var typ = this.typ;
            var sceneid = settingData.local_table[gameData.mapId] || 0;
            //暂时设置0
            sceneid = 0;
            if (typ == 1) {
                this.loadTexture(zipai.res['BigPaiBG_' + sceneid]);
                this.setContentSize(cc.size(89, 248));
            } else {
                this.loadTexture(zipai.res['huxiBG' + sceneid]);
            }
        },
        onStatusChange: function (event) {
            if (this.checkNoRunning()) return;
            var data = event.getUserData();

        },
        onSelect: function (sender, type) {
            switch (type) {
                case ccui.Widget.TOUCH_BEGAN:
                    this.isSelected = true;
                    this.setOpacity(180);
                    this.selectedCard.setData(this.data);
                    var pos = sender.getTouchBeganPosition();
                    this.updateSelectedCard(true, pos);

                    this.p.setSameCardShow(this.data, true);
                    break;

                case ccui.Widget.TOUCH_MOVED:
                    var pos = sender.getTouchMovePosition();
                    this.updateSelectedCard(true, pos);
                    break;

                case ccui.Widget.TOUCH_ENDED:
                    this.isSelected = false;
                    this.setOpacity(255);
                    var pos = sender.getTouchEndPosition();
                    this.lastClickTime = Date.now();
                    this.updateSelectedCard(false, pos);

                    this.p.setSameCardShow(this.data, false);
                    break;

                case ccui.Widget.TOUCH_CANCELED:
                    this.isSelected = false;
                    this.setOpacity(255);
                    var pos = sender.getTouchEndPosition();
                    this.updateSelectedCard(false, pos);

                    this.p.setSameCardShow(this.data, false);
                    break;

                default:
                    break;
            }
            return false;
        },
        updateSelectedCard: function (isShow, pos) {
            this.selectedCard.setVisible(isShow);
            var px = pos.x;
            var py = pos.y;
            this.selectedCard.setPosition(px, py);

            if (isShow == false) {
                if (py > 480)
                    this.showCard();
                else {
                    this.p.moveCard(this, px, py);
                }
            }
        },
        showCard: function () {
            if (this.p.isTurnOut == true) {
                this.p.userShowCard(this.data, false, this.row, this.column);
            } else {
                // HUD.showMessage("未到出牌回合！");
            }
        },
        setOutCardray: function () {
            var select = this.getChildByName('out');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(75, 75, 75));
                select.setOpacity(80);
                var size = this.getContentSize();
                if(size.width == 0){
                    var cardSize = mCard.getSmallSize();
                    size = cc.size(cardSize.w,cardSize.h);
                }
                select.setContentSize(size);
                select.setPosition(cc.p(0, 0));
                this.addChild(select);
            }
            select.setVisible(true);

        },
        setCardGray: function (flag) {
            var select = this.getChildByName('graycard');
            if (!select) {
                select = new cc.LayerColor();
                select.setColor(cc.color(75, 75, 75));
                select.setOpacity(80);
                var size = this.getContentSize();
                if(size.width == 0){
                    var cardSize = mCard.getSmallSize();
                    size = cc.size(cardSize.w,cardSize.h);
                }
                select.setContentSize(size);
                select.setPosition(cc.p(0, 0));
                select.setName('graycard');
                this.addChild(select);
            }
            select.setVisible(flag);

        },
        setKan: function (isKan) {
            this.isKan = isKan;
            this.setGrayCard(isKan);
        },
        setTingMark: function (flag) {
            var mark = this.getChildByName('tingmark');
            if (!mark) {
                mark = new cc.Sprite(zipai.res.tingmark_png);
                mark.setAnchorPoint(1,1);
                mark.setName('tingmark');
                this.addChild(mark);
            }
            var fontType = mCard.getCardFontType();
            var cardSize = mCard.getCardSize();
            mark.setPosition(cardSize.w, fontType == 2?cardSize.h+20:cardSize.h);
            mark.setVisible(flag);
        },
        setHuMark: function (_cardSize) {
            var mark = this.getChildByName('humark');
            if (!mark) {
                mark = new cc.Sprite(zipai.res.humark_png);
                mark.setAnchorPoint(1,1);
                mark.setName('humark');
                mark.setScale(0.8)
                this.addChild(mark,100);
            }
            var fontType = mCard.getCardFontType();
            var cardSize = mCard.getCardSize();
            mark.setPosition(_cardSize, _cardSize);
            // mark.setPosition(1, 1);
            mark.setVisible(true);
        }
    });
    exports.Card = Card;
})(window);